package com.imooc.sell.utils.pyhutils;

import com.imooc.sell.controller.RemarkController;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.security.spec.ECField;

/**
 * 打印类，打印被访问信息以及接口信息
 */
@Aspect
@Component
public class RemarkAspect {
    private static final Logger mlogger = LoggerFactory.getLogger(RemarkController.class);

    @Before("execution(public * com.imooc.sell.controller.RemarkController.*(..))")
    public void begin(JoinPoint joinPoint) {
        ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest hsr = sra.getRequest();
        //url
        mlogger.info("url:{}", hsr.getRequestURL());
        //method
        mlogger.info("请求方式:{}", hsr.getMethod());
        //ip
        mlogger.info("ip:{}", hsr.getRemoteAddr());
        //接口
        mlogger.info("访问接口:{}", joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
        //参数
        mlogger.info("参数:{}", joinPoint.getArgs());


    }

    @Before("execution(public * com.imooc.sell.controller.RemarkController.updataRemark(..))")
    public void toDate(JoinPoint joinPoint) {
        ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest hsr = sra.getRequest();
        //url
        mlogger.info("url:{}", hsr.getRequestURL());
        //method
        mlogger.info("请求方式:{}", hsr.getMethod());
        //ip
        mlogger.info("ip:{}", hsr.getRemoteAddr());
        //接口
        mlogger.info("访问接口:{}", joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
        //参数
        mlogger.info("参数:{}", joinPoint.getArgs());

        try {
            mlogger.info("参数:{}", hsr.getInputStream());
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @After("execution(public * com.imooc.sell.controller.RemarkController.*(..))")
    public void end() {
    }

}
